$(function() {
	
	$("#showEInvoiceList").click(function(){
		var contractId = sessionStorage.contractId;
		console.log(contractId);
		padMarketing.ajaxNormal(
				'/pad_marketing/myPolicy/getEInvoiceList.action',
				{'contractId':contractId},
				function(data){
					console.log(data);
					/*var url="https://pts.allianz.com.cn/Einvoice/evat?filename=VCIyIJdXG6QpPtpaPcd24PRV90PS5pLJ1PSjczo%3D";
					var emailaddress="jiakuan.zhu@newtouch.cn";
					var html = '';
					html+='<tr>';
				    html+='<td>1</td>';
					html+='<td>2012-11-12</td>';//发票打印日期
					html+='<td>200,00,00</td>';//发票金额
					html+='<td>';
					html+='<a  class="icon icon-circle-notebook" onclick="openEInvoice(\'' + $.trim(url) + '\')" ></a>';//发票地址
					html+='<a  class="icon icon-circle-you" onclick="openEmail(\''+ $.trim(url) +'\',\''+ $.trim(emailaddress) +'\')"></a>';//发邮件
					html+='</td>';
					html+='</tr>';
					$("#eInvoiceTb tbody").html(html);
					showDiv('open2');*/
					if(data.length>0){
						var html='';
						for(var i=0;i<data.length;i++){
							html+='<tr>';
						    html+='<td>'+(i+1)+'</td>';
							html+='<td>'+$.trim(data[i].INVOICE_DATE?new Date(data[i].INVOICE_DATE).Format("yyyy-MM-dd"):'')+'</td>';//发票打印日期
							html+='<td>'+$.trim(data[i].INVOICE_AMOUNT)+'</td>';//发票金额
							html+='<td>';
							html+='<a  class="icon icon-circle-notebook" onclick="openEInvoice(\'' + $.trim(data[i].INVOICE_URL) + '\')" ></a>';//发票地址
							html+='<a  class="icon icon-circle-you" onclick="openEmail(\''+ $.trim(data[i].INVOICE_URL) +'\')"></a>';//发邮件
							html+='</td>';
							html+='</tr>';
						}
						$("#eInvoiceTb tbody").html(html);
						showDiv('open2');
					}else{
						alertMsg('没有电子发票');
					}
				},
				function(){
					
				},
				true,
				true,
				'呦!你的电子发票很多哦!'
		)
	});
	//点击基本信息tab页隐藏电子发票按钮
	$("#myTab1_title0").hide();
	
	//发送邮件方法
	$("#sendMail").click(function(){
		var pdfUrl = $("#pdfUrl").val();
		var receiveEmailAddress = $("#receiveEmailAddress").text();
		var sendEmailAddress = "Einvoice@allianz.com.cn";
		if(receiveEmailAddress !=null && receiveEmailAddress !="" && receiveEmailAddress != undefined){
			padMarketing.ajaxNormal(
					'/pad_marketing/myPolicy/sendEmail.action',
					{'pdfUrl':pdfUrl,'receiveEmailAddress':receiveEmailAddress,'sendEmailAddress':sendEmailAddress},
					function(data){
						console.log(data);
						if(data){
							closeDiv('open3');
						}
					},
					function(){
						
					},
					true,
					true,
					'邮件正在发送中'
			)
		}
	});
	
});
//预览pdf方法
function openEInvoice(url){
//	window.open(url);
	//调壳弹出新页面，参数说明：1：回调函数名称，2：要展示的地址，3：回调函数参数，4：是否需要签字
	gotoPDF('pdfRefreshs',url,0,'N');
}

function openEmail(pdfUrl,receiveEmailAddress){
	$("#pdfUrl").val(pdfUrl);
	$("#receiveEmailAddress").text(receiveEmailAddress);
//	var receiveEmailAddress = $("#receiveEmailAddress").text();
//	receiveEmailAddress = "";
	if(receiveEmailAddress == null || receiveEmailAddress == "" || receiveEmailAddress == undefined){
		var html='<font style="color:red">投保人未留下邮箱，请通过公司保全变更或客户自助服务网站为投保人增加邮箱</font>';
		$("#someTip").html(html);
		$("#sendMail").unbind();
	}
	showDiv('open3');
}